package com.lninl.controller;

import com.lninl.model.Tag;
import com.lninl.repository.TagRepository;
import com.lninl.service.TagService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Controller
public class TagController {

    @Autowired
    private TagService service;

    @Autowired
    private TagRepository repository;

    /**
     * 根据名字返回标签信息，如果不存在则创建之后再返回。
     *
     * @param name
     * @return
     */
    @RequestMapping(value = "/tag/{name}/name")
    @ResponseBody
    public Tag getByName(@PathVariable String name) {
        Tag tag = repository.findByName(name);

        if (tag == null) {
            tag = new Tag();
            tag.setName(name);
            tag.setCreated(new DateTime());
            tag.setStatus(Tag.NORMAL);
            return repository.save(tag);
        }

        return tag;
    }

    /**
     * 根据id得到标签信息
     *
     * @param tag
     * @return
     */
    @RequestMapping(value = "/tag/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Tag get(@PathVariable("id") Tag tag) {
        return tag;
    }

    /**
     * 添加标签
     *
     * @param tag
     * @param bindingResult
     * @param model
     * @return
     */
    @RequestMapping(value = "/tag", method = RequestMethod.POST, consumes = "application/json")
    public ResponseEntity add(@RequestBody @Valid Tag tag, BindingResult bindingResult,
                              Model model) {
        if (bindingResult.hasErrors()) {
            model.addAttribute(tag);
            return new ResponseEntity<>("{"
                    + bindingResult.getFieldError().getField() + ":"
                    + bindingResult.getFieldError().getCode() + "}",
                    HttpStatus.OK);
        }
        tag.setCreated(new DateTime());
        tag.setStatus(1);
        return new ResponseEntity<>(repository.save(tag), HttpStatus.OK);
    }

    /**
     * 得到标签列表
     *
     * @param pageable
     * @return
     */
    @RequestMapping(value = "/tag", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public Page<Tag> tags(Pageable pageable) {
        return repository.findAll(pageable);
    }

    /**
     * 更新标签
     *
     * @param oldTag
     * @param tag
     * @param bindingResult
     * @param model
     * @return
     */
    @RequestMapping(value = "/tag/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public ResponseEntity update(@PathVariable("id") Tag oldTag,@RequestBody @Valid Tag tag,
                                 BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            return new ResponseEntity<>("{"
                    + bindingResult.getFieldError().getField() + ":"
                    + bindingResult.getFieldError().getCode() + "}",
                    HttpStatus.METHOD_NOT_ALLOWED);
        }
        if (tag.getName() != null && tag.getName().length() > 0) {
            oldTag.setName(tag.getName());
        }
        if (null != tag.getStatus()) {
            oldTag.setStatus(tag.getStatus());
        }
        if (null != tag.getModified()) {
            oldTag.setModified(new DateTime());
        }
        return new ResponseEntity<>(repository.save(oldTag), HttpStatus.OK);
    }

    /**
     * 删除标签
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/tag/{id}", method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable Long id, Model model) {
        try {
            repository.delete(id);
        } catch (Exception e) {
        }
        return new ResponseEntity<>(null, HttpStatus.OK);
    }


}
